package com.ycsj.goldmedalnewconcept.common.util;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.blankj.utilcode.util.LogUtils;
import com.ycsj.common.manager.BqaManager;
import com.ycsj.goldmedalnewconcept.common.Constants;
import com.ycsj.goldmedalnewconcept.common.HttpAddress;
import com.ycsj.goldmedalnewconcept.common.bean.EventInfo;
import com.ycsj.goldmedalnewconcept.common.http.HttpCallBack;
import com.ycsj.goldmedalnewconcept.common.http.HttpUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 请求活动的公共请求接口
 * Created by Administrator on 2019/4/19 0019.
 */

public class EventUtils {

    //分页
    private static int page = 1;

    //请求的地址
    private static String url = "";

    //请求的账号
    private static String acc = "";

    //请求的角色
    private static String role = "";

    //请求的token
    private static String apptoken = "";


    /**
     * 公共的请求活动数据的方法
     *
     * @param context   上下文
     * @param method    正常请求,下拉刷新,加载更多
     * @param classNum  教师端请求活动时需要传入的班级号
     * @param listener  请求数据后的回调
     * @param teacherId 这个参数主要用来校长端查看全部活动和指定老师的活动,老师端和家长端调用该请求时传入""即可
     */
    public static void httpEvent(Context context, final int method, String classNum, String teacherId, final OnRequestListener listener) {

        if (method == BqaManager.NORML || method == BqaManager.PULLREFRESH) {
            page = 1;
        } else {
            page++;
        }

        role = AccountUtils.getRole(context);
        acc = AccountUtils.getAccount(context);
        apptoken = AccountUtils.getToken(context);

        Map<String, String> map = new HashMap<>();

        if ("2".equals(role)) {
            //老师
            map.put("type", "11");
            map.put("classnum", classNum);
            url = HttpAddress.TEACHEREVENT;
        } else if ("3".equals(role)) {
            //校长
            if (TextUtils.isEmpty(teacherId)) {
                //查看全部
                map.put("type", "2");
                url = HttpAddress.MASTEREVENT;
            } else {
                //校长查看指定老师的活动 调用的是 老师查看活动的接口
                map.put("type", "11");
                map.put("classnum", "");
                url = HttpAddress.TEACHEREVENT;
                role = "2";
                acc = teacherId;
            }

        } else if ("1".equals(role)) {
            //家长
            map.put("type", "5");
            url = HttpAddress.PARENNTEVENT;
        }

        map.put("acc", acc);
        map.put("role", role);
        map.put("apptoken", apptoken);
        map.put("platform", "2");
        map.put("page", page + "");

        HttpUtils.post(url, map, context, new HttpCallBack<EventInfo>() {
            @Override
            public void onSuccess(EventInfo info) {
                if (null != info) {
                    if ("200".equals(info.getState())) {

                        //返回成功
                        List<EventInfo.ListBean> newDatas = info.getList();

                        if (newDatas != null) {
                            if (listener != null) {
                                listener.onSuccess(method, newDatas);
                            }
                        }

                    } else {
                        if (listener != null) {
                            listener.onStateError(method);
                        }
                        StateCodeUtils.showSateCode(info.getState());
                        LogUtils.iTag("test", "状态码是:" + info.getState());
                        Log.i("test", Constants.EMPTY);
                    }
                }
            }

            @Override
            public void onError(String e) {
                if (listener != null) {
                    listener.onError(method);
                }
            }
        });
    }


    /**
     * 请求数据后的回调接口
     */
    public interface OnRequestListener {
        void onError(int method);

        void onSuccess(int method, List<EventInfo.ListBean> newDatas);

        void onStateError(int method);
    }

}
